﻿namespace RegexMaster
{
    using System;
    using System.Drawing;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    using Properties;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender,
                                              EventArgs e)
        {
            ProcessRegex();
        }

        private void ProcessRegex()
        {
            SetDefaultView();

            richSearchResult.Text = string.Empty;
            string searchText = richSearchText.Text;
            string pattern = richRegEx.Text;
            if (string.IsNullOrEmpty(pattern))
            {
                return;
            }

            RegexOptions options = RegexOptions.Singleline;
            if (cbIsMultiline.Checked)
            {
                options = RegexOptions.Multiline;
            }

            string[] result;
            try
            {
                result = Regex.Split(searchText,
                                     pattern,
                                     options);
            }
            catch (Exception)
            {
                richSearchResult.Text = "Error while parsing...";
                return;
            }

            foreach (string s in result)
            {
                richSearchResult.Text += s + Environment.NewLine;
            }
            Match m = Regex.Match(searchText,
                                  pattern,
                                  options);

            while (m.Success)
            {
                SelectTextInRichEditor(m);
                m = m.NextMatch();
            }
        }

        private void SelectTextInRichEditor(Capture m)
        {
            richSearchText.Select(m.Index, m.Length);
            richSearchText.SelectionColor = Color.Red;
            richSearchText.SelectionFont = new Font("Arial", 14, FontStyle.Bold);
            richSearchText.SelectionBackColor = Color.YellowGreen;
        }

        private void SetDefaultView()
        {
            richSearchText.SelectAll();
            richSearchText.SelectionColor = Color.Black;
            richSearchText.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
            richSearchText.SelectionBackColor = Color.White;
        }

        private void Form1_Load(object sender,
                                EventArgs e)
        {
            LoadAllData();
            ProcessRegex();
        }

        private void Form1_FormClosed(object sender,
                                      FormClosedEventArgs e)
        {
            SaveAllData();
        }

        private void SaveAllData()
        {
            Settings.Default.Pattern = richRegEx.Text;
            Settings.Default.SourceText = richSearchText.Text;
            Settings.Default.Save();
        }

        private void LoadAllData()
        {
            richRegEx.Text = Settings.Default.Pattern;
            richSearchText.Text = Settings.Default.SourceText;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox box = new AboutBox();
            box.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cbIsMultiline_CheckedChanged(object sender, EventArgs e)
        {
            ProcessRegex();
        }
    }
}